const Router=require('koa-router');
const message=new Router();
const sqldata = require('./sqldata');
message.get('/',async(ctx)=>{
    let mydata1=[];
    let mydata2=[];
    console.log(ctx.request.query)
    let mydata = await new Promise((resolve, reject)=>{
        let sql=`select * from user,talking where user.Uid=talking.Bid and talking.Uid='${ctx.request.query.Uid}'`
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    if(mydata.length>1){
        for(let i=0;i<mydata.length-1;i++){
            if(mydata[i].Bid!=mydata[i+1].Bid){
                mydata1.push(mydata[i])
            }else if(i==mydata.length-2){
                mydata1.push(mydata[i+1])
            }
        }
        for(let j=0;j<mydata1.length-1;j++){
            for(let h=0;h<mydata1.length-j-1;h++){
                if(mydata1[h].Ttime<mydata1[h+1].Ttime){
                    let temp=mydata1[h+1];
                    mydata1[h+1]=mydata1[h];
                    mydata1[h]=temp;
                }
            }
        }
    }else{
        mydata1=mydata
    }
    console.log('///////////////')
    console.log(mydata1)
    for(let k=0;k<mydata1.length;k++){
        let time=mydata1[k].Ttime
        let year = time.getFullYear()
        let month = time.getMonth() + 1
        let date = time.getDate()
        let hours = time.getHours()
        let minute = time.getMinutes()
        let second = time.getSeconds()
        if (month < 10) { month = '0' + month }
        if (date < 10) { date = '0' + date }
        if (hours < 10) { hours = '0' + hours }
        if (minute < 10) { minute = '0' + minute }
        if (second < 10) { second = '0' + second }
        mydata1[k].Ttime=year + '-' + month + '-' + date + ' ' + hours + ':' + minute + ':' + second
    }
    ctx.body=mydata1;
})
message.get('/me-talking',async(ctx)=>{
    let sql;
    let mydata1;
    let mydata2;
    console.log(ctx.request.query)
    mydata1 = await new Promise((resolve, reject)=>{
        sql=`select * from user,talking where talking.Uid=user.Uid and talking.Uid='${ctx.request.query.Uid}' and talking.Bid='${ctx.request.query.Bid}'`
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    mydata2= await new Promise((resolve, reject)=>{
        sql=`select * from user,talking where talking.Uid=user.Uid and talking.Uid='${ctx.request.query.Bid}' and talking.Bid='${ctx.request.query.Uid}'`
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    for(let i=0;i<mydata2.length;i++){
        mydata1.push(mydata2[i]);
    }
    for(let j=0;j<mydata1.length-1;j++){
        for(let h=0;h<mydata1.length-j-1;h++){
            if(mydata1[h].Ttime>mydata1[h+1].Ttime){
                var temp=mydata1[h];
                mydata1[h]=mydata1[h+1];
                mydata1[h+1]=temp
            }
        }
    }
    ctx.body=mydata1;
})
message.post('/me-talking',async(ctx)=>{
    let val=ctx.request.body
    let mydata = await new Promise((resolve, reject)=>{
        let sql=`insert into talking values('${val.Uid}','${val.Bid}','${val.Ttime}','${val.Ttext}')`
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    ctx.body=mydata;
})
//关注页面
message.get('/me-attended',async(ctx)=>{
    let val=ctx.request.query;
    let mydata = await new Promise((resolve, reject)=>{
        let sql=`select * from user,attented where user.Uid=attented.Bid and attented.Uid='${val.Uid}'`
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    for(let i=0;i<mydata.length;i++){
        if(mydata[i].Astate=='+关注'){
            mydata[i].color='#ff986c'
        }else{
            mydata[i].color='lightgray'
        }
    }
    ctx.body=mydata;
})
message.post('/me-attended',async(ctx)=>{
    console.log(ctx.request.body)
    let val=ctx.request.body;
    let mydata;
    let sql;
    if(val.Astate=='已关注'){
        mydata= await new Promise((resolve, reject)=>{
            sql=`insert into attented values ('${val.Uid}','${val.Bid}','${val.Time}','已关注')`
            return sqldata(sql, (err, data)=>{
                if(err) reject(err);
                resolve(data);	// 返回拿到的数据
            })
        })
        mydata= await new Promise((resolve, reject)=>{
            sql=`update attented  set Time='${val.Time}',Astate='${val.Astate}' where Uid='${val.Bid}' and Bid='${val.Uid}'`
            return sqldata(sql, (err, data)=>{
                if(err) reject(err);
                resolve(data);	// 返回拿到的数据
            })
        })
        mydata= await new Promise((resolve, reject)=>{
            sql=`insert into attention values ('${val.Uid}','${val.Bid}','${val.Astate}')`
            return sqldata(sql, (err, data)=>{
                if(err) reject(err);
                resolve(data);	// 返回拿到的数据
            })
        })
    }else{      
        mydata = await new Promise((resolve, reject)=>{
            sql=`delete from attented where Uid='${val.Uid}' and Bid='${val.Bid}'`
            return sqldata(sql, (err, data)=>{
                if(err) reject(err);
                resolve(data);	// 返回拿到的数据
            })
        })
    
        mydata= await new Promise((resolve, reject)=>{
            sql=`update attented  set Time='${val.Time}',Astate='${val.Astate}' where Uid='${val.Bid}' and Bid='${val.Uid}'`
            return sqldata(sql, (err, data)=>{
                if(err) reject(err);
                resolve(data);	// 返回拿到的数据
            })
        })
        mydata = await new Promise((resolve, reject)=>{
            sql=`delete from attention where Uid='${val.Uid}' and Bid='${val.Bid}'`
            return sqldata(sql, (err, data)=>{
                if(err) reject(err);
                resolve(data);	// 返回拿到的数据
            })
        })
    }
})
//评论页面
message.get('/me-comment',async(ctx)=>{
    let sql;
    let mydata = await new Promise((resolve, reject)=>{
        sql=`select comment.Cid,comment.Uid,comment.Ctime,comment.Ctext,user.Uid,user.Uname,user.Uimage,works.Wid,works.Wtitle from comment,user,works 
        where comment.Wid = works.Wid and user.Uid=comment.Uid and works.Uid='${ctx.request.query.Uid}'`;
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    let mydata1 = await new Promise((resolve, reject)=>{
        sql=`select * from comment,commentfabulous where comment.Cid=commentfabulous.Cid and commentfabulous.Uid='${ctx.request.query.Uid}'`;
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    if(mydata1!=''){
        for(let i=0;i<mydata.length;i++){
            mydata[i].img='https://s3.bmp.ovh/imgs/2021/11/8f8308b8016421b4.png'
            for(let j=0;j<mydata1.length;j++){
                if(mydata[i].Cid==mydata1[j].Cid){
                    mydata[i].img='https://s3.bmp.ovh/imgs/2021/11/2d7cde86aa7b7457.png'
                }
            }
        }
    }else{
        for(let k=0;k<mydata.length;k++){
            mydata[k].img='https://s3.bmp.ovh/imgs/2021/11/8f8308b8016421b4.png'
        }
    }   
    ctx.body=mydata;
})
message.post('/me-comment',async(ctx)=>{
    let sql;
    let val=ctx.request.body;
    let mydata;
    let mydata1;
    if(val.img=='https://s3.bmp.ovh/imgs/2021/11/2d7cde86aa7b7457.png'){
        mydata = await new Promise((resolve, reject)=>{
            sql=`update comment set Czan=Czan+1 where Cid='${val.Cid}'`
            return sqldata(sql, (err, data)=>{
                if(err) reject(err);
                resolve(data);	// 返回拿到的数据
            })
        })
        mydata1 = await new Promise((resolve, reject)=>{
            sql=`insert into commentfabulous values('${val.Cid}','${val.Uid}','${val.Ctime}')`
            return sqldata(sql, (err, data)=>{
                if(err) reject(err);
                resolve(data);	// 返回拿到的数据
            })
        })
    }else{
        mydata = await new Promise((resolve, reject)=>{
            sql=`update comment set Czan=Czan-1 where Cid='${val.Cid}'`
            return sqldata(sql, (err, data)=>{
                if(err) reject(err);
                resolve(data);	// 返回拿到的数据
            })
        })
        mydata1 = await new Promise((resolve, reject)=>{
            sql=`delete from commentfabulous where Cid='${val.Cid}' and Uid='${val.Uid}'`
            return sqldata(sql, (err, data)=>{
                if(err) reject(err);
                resolve(data);	// 返回拿到的数据
            })
        })
    }
    
})
//赞页面
message.get('/me-liking',async(ctx)=>{
    let sql
    let mydata;
    let mydata1;
    let mydata2=[];
    mydata = await new Promise((resolve, reject)=>{
        sql=`select * from user,works,workfabulous 
        where works.Uid='${ctx.request.query.Uid}'
        and workfabulous.Wid=works.Wid and user.Uid=workfabulous.Uid`
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    mydata1 = await new Promise((resolve, reject)=>{
        sql=`select * from user,works,comment,commentfabulous 
        where comment.Uid='${ctx.request.query.Uid}'
        and commentfabulous.Cid=comment.Cid and user.Uid=commentfabulous.Uid and comment.Wid=works.Wid`
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    
    for(let i=0;i<mydata.length;i++){
        mydata[i].types='作品'
        mydata[i].time=mydata[i].Wtime
        mydata2.push(mydata[i])
    }
    for(let j=0;j<mydata1.length;j++){
        mydata1[j].types='评论'
        mydata1[j].time=mydata1[j].Ctime
        mydata2.push(mydata1[j])
    }
    for(let k=0;k<mydata2.length;k++){
        if(!mydata2[k].Ctext){
            mydata2[k].text=mydata2[k].Wtitle
        }else{
            mydata2[k].text=mydata2[k].Ctext
        }
    }
    ctx.body = mydata2;
})
module.exports=message;